package liming.ai.code.java_maven.info;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class FileCenter {
    //模块名字-包名-类名-基本信息实体类
    Map<String, Map<String,Map<String,JavaInfo>>> javaInfosMap = new HashMap<>();


    public boolean containsKey(String modeName,String packageName,String className,String FileType){
        return javaInfosMap.containsKey(modeName)&&
                javaInfosMap.get(modeName).containsKey(packageName)&&
                javaInfosMap.get(modeName).get(packageName).containsKey(className)&&
                containsType(javaInfosMap.get(modeName).get(packageName).get(className),FileType);
    }

    boolean containsType(JavaInfo javaInfo,String FileType){
        switch (FileType){
            case "java":
                return javaInfo.getJavaFile().exists();
            case "md":
                return javaInfo.getMdFile().exists();
            default:
                return false;
        }
    }

    // 添加文件记录
    public void putJavaInfo(String modeName, String packageName,
                            String className, JavaInfo javaInfo) {
        javaInfosMap.computeIfAbsent(modeName, k -> new HashMap<>())
                .computeIfAbsent(packageName, k -> new HashMap<>())
                .put(className, javaInfo);
    }

    // 删除文件记录
    public void removeJavaInfo(String modeName, String packageName, String className) {
        if(javaInfosMap.containsKey(modeName)) {
            Map<String, Map<String, JavaInfo>> packageMap = javaInfosMap.get(modeName);
            if(packageMap.containsKey(packageName)) {
                packageMap.get(packageName).remove(className);

                // 清理空包
                if(packageMap.get(packageName).isEmpty()) {
                    packageMap.remove(packageName);
                }
            }
            // 清理空模块
            if(packageMap.isEmpty()) {
                javaInfosMap.remove(modeName);
            }
        }
    }
}
